package com.game.server;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;

import org.apache.log4j.Logger;

import com.game.majiang.config.LogManager;


/**
 * 初始化服务器组
 * @author 
 *
 */
@Data
public class InitServers {

    private List<IServer> serverList;

    public void Start(String args[]) {
        serverList = new ArrayList<IServer>();
        String path = args[0];
        LogManager.getInstance().Log("加载配置文件："+path);
        try {
            for (ServerConfig config : ConfigManager.getInstance().getServer(path)) {
         
            	LogManager.getInstance().Log("开始启动" + config.getName());
                @SuppressWarnings("rawtypes")
                Class cal = Class.forName(config.getClassName());
                IServer server = (IServer) cal.newInstance();
                server.readConfig(config.getConfigUrl());
                server.init();
                server.bind(new Short(config.getPort()));
                server.finish();
                serverList.add(server);
                LogManager.getInstance().Log("启动【" + config.getName() + "】结束");
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogManager.getInstance().Error("服务器组启动失败");
            System.exit(1);
        }
    }

    public void stop() {
        for (IServer server : serverList) {
            server.close();
        }

    }

}
